<template>
  <!-- 导航 -->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>设备管理</el-breadcrumb-item>
        <el-breadcrumb-item>设备类型管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-form :model="deviceMainPlanQuery" inline>
        <el-form-item label="计划编号">
          <el-input v-model="deviceMainPlanQuery.planCode"/>
        </el-form-item>
        <el-form-item label="计划名称">
          <el-input v-model="deviceMainPlanQuery.planName"/>
        </el-form-item>
        <el-form-item label="计划类型">
          <el-select v-model="deviceMainPlanQuery.planType" placeholder="请选择计划类型" style="width: 150px;">
            <el-option label="全部" value=""></el-option>
            <el-option label="设备保养" value="1"></el-option>
            <el-option label="设备检点" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划状态">
          <el-select v-model="deviceMainPlanQuery.planStatus" placeholder="请选择状态" style="width: 150px;">
            <el-option label="所有" value=""></el-option>
            <el-option label="草稿" value="0"></el-option>
            <el-option label="已确认" value="1"></el-option>
            <el-option label="审批中" value="2"></el-option>
            <el-option label="已审批" value="3"></el-option>
            <el-option label="已完成" value="4"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="loadTasks(0)" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
  <el-row style="margin: 0px 10px;background-color: ghostwhite;padding: 0px 10px">
    <el-col :span="24">
      <el-button-group>
        <el-button type="primary" @click="addTask" >新增</el-button>
        <el-button type="primary" @click="batchEditTask">编辑</el-button>
        <el-button type="primary" @click="batchDeleteTask">删除</el-button>
      </el-button-group>
    </el-col>
  </el-row>
  <!-- 数据列表 -->
  <el-row style="margin: 0px 10px;padding: 0px 15px">
    <el-col :span="24">
      <el-table :data="deviceMainPlans" border stripe
                row-key="projectCode"
                ref="taskTable">
        <el-table-column prop="planCode" label="计划编号">
          <template v-slot="scope">
            <a href="#" @click.prevent="openDialog(scope.row)">
              {{ scope.row.planCode }}
            </a>
          </template>
        </el-table-column>
        <el-table-column prop="planName" label="计划名称"/>
        <el-table-column prop="planType" label="计划类型">
          <template v-slot="scope">
      <span>
        {{ scope.row.planType == 1 ? '设备保养' : '设备检点' }}
      </span>
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="开始日期"/>
        <el-table-column prop="endDate" label="结束日期"/>
        <el-table-column prop="frequency" label="频率"/>
        <el-table-column prop="planStatus" label="状态">
          <template v-slot="scope">
            <span v-if="scope.row.planStatus == 0">草稿</span>
            <span v-else-if="scope.row.planStatus == 1">已确认</span>
            <span v-else-if="scope.row.planStatus == 2">审批中</span>
            <span v-else-if="scope.row.planStatus == 3">已审批</span>
            <span v-else-if="scope.row.planStatus == 4">已完成</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-link  style="margin: 0px 5px" type="primary" @click="openEditDialog(scope.row)" v-if="scope.row.planStatus != 4"><el-icon><EditPen /></el-icon>编辑</el-link>
            <el-link  style="margin: 0px 5px" type="primary" @click="deleteTask(scope.row)" v-if="scope.row.planStatus != 4"><el-icon><Delete /></el-icon>删除</el-link>
            <el-link  style="margin: 0px 5px" type="primary" @click="openEditDialog(scope.row)" v-if="scope.row.planStatus == 4"><el-icon><Delete /></el-icon>停用</el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>

  <el-dialog
      title="新增任务"
      v-model="dialogVisible"
      width="50%"
      @close="resetForm">
    <el-form :model="deviceMainPlanFrom" ref="taskFormRef" label-width="120px">
      <!-- 任务标题 -->
      <el-form-item label="计划编码" prop="planCode">
        <el-input v-model="deviceMainPlanFrom.planCode" placeholder="请输入计划编码"/>
        <el-button type="primary" @click="generateDeviceCode">生成编码</el-button>
      </el-form-item>
      <!-- 任务工期 -->
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="deviceMainPlanFrom.planName" placeholder="请输入计划名称" />
      </el-form-item>
      <el-form-item label="计划类型" prop="planType">
        <el-radio-group v-model="deviceMainPlanFrom.planType">
          <el-radio :label="1">设备保养</el-radio>
          <el-radio :label="2">设备检点</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="频率" prop="frequencyAmount">
        <el-input v-model="deviceMainPlanFrom.frequencyAmount" placeholder="请输入频率"></el-input>
      </el-form-item>

      <el-form-item label="频率单位" prop="frequencyUnit">
        <el-select v-model="deviceMainPlanFrom.frequencyUnit" placeholder="请选择频率单位">
          <el-option label="次/天" value="天"></el-option>
          <el-option label="次/周" value="周"></el-option>
          <el-option label="次/月" value="月"></el-option>
          <el-option label="次/年" value="年"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间" prop="startDate">
        <el-date-picker
            v-model="deviceMainPlanFrom.startDate"
            type="datetime"
            placeholder="选择开始时间"
            :clearable="true">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker
            v-model="deviceMainPlanFrom.endDate"
            type="datetime"
            placeholder="选择结束时间"
            :clearable="true">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="状态" prop="planStatus">
        <el-select v-model="deviceMainPlanFrom.planStatus" placeholder="请选择状态">
          <el-option label="草稿" value="0"></el-option>
          <el-option label="已确认" value="1"></el-option>
          <el-option label="审批中" value="2"></el-option>
          <el-option label="已审批" value="3"></el-option>
          <el-option label="已完成" value="4"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注" prop="remarks">
        <el-input v-model="deviceMainPlanFrom.remarks" type="textarea" placeholder="请输入备注"></el-input>
      </el-form-item>
      <!-- 表单操作按钮 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <!--编辑任务-->
  <el-dialog
      title="编辑任务"
      v-model="EditdialogVisible"
      width="50%"
      @close="resetForm">
    <el-form :model="deviceMainPlanFrom" ref="taskFormRef" label-width="120px">
      <!-- 任务标题 -->
      <el-form-item label="计划编码" prop="planCode">
        <el-input v-model="deviceMainPlanFrom.planCode" placeholder="请输入计划编码"/>
        <el-button type="primary" @click="generateDeviceCode">生成编码</el-button>
      </el-form-item>
      <!-- 任务工期 -->
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="deviceMainPlanFrom.planName" placeholder="请输入计划名称" />
      </el-form-item>
      <el-form-item label="计划类型" prop="planType">
        <el-radio-group v-model="deviceMainPlanFrom.planType">
          <el-radio :label="1">设备保养</el-radio>
          <el-radio :label="2">设备检点</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="频率" prop="frequencyAmount">
        <el-input v-model="deviceMainPlanFrom.frequencyAmount" placeholder="请输入频率"></el-input>
      </el-form-item>

      <el-form-item label="频率单位" prop="frequencyUnit">
        <el-select v-model="deviceMainPlanFrom.frequencyUnit" placeholder="请选择频率单位">
          <el-option label="次/天" value="天"></el-option>
          <el-option label="次/周" value="周"></el-option>
          <el-option label="次/月" value="月"></el-option>
          <el-option label="次/年" value="年"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间" prop="startDate">
        <el-date-picker
            v-model="deviceMainPlanFrom.startDate"
            type="datetime"
            placeholder="选择开始时间"
            :clearable="true">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker
            v-model="deviceMainPlanFrom.endDate"
            type="datetime"
            placeholder="选择结束时间"
            :clearable="true">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="状态" prop="planStatus">
        <el-select v-model="deviceMainPlanFrom.planStatus" placeholder="请选择状态">
          <el-option label="草稿" value="0"></el-option>
          <el-option label="已确认" value="1"></el-option>
          <el-option label="审批中" value="2"></el-option>
          <el-option label="已审批" value="3"></el-option>
          <el-option label="已完成" value="4"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注" prop="remarks">
        <el-input v-model="deviceMainPlanFrom.remarks" type="textarea" placeholder="请输入备注"></el-input>
      </el-form-item>

      <el-tabs type="border-card">
        <el-tab-pane label="设备清单">
          <el-button type="primary" @click="addMainPlanOfledgers" v-if="isDialog">新增</el-button>
          <el-button type="danger" @click="deleteSelectionPlanOfledger" v-if="isDialog" >删除</el-button>
          <el-table :data="deviceMainPlanOfledgers" style="margin-top: 20px;" @selection-change="handleSelectionChangeofledger">
            <el-table-column
                type="selection"
                width="55">
            </el-table-column>
            <el-table-column prop="deviceCode" label="设备编码" width="130"></el-table-column>
            <el-table-column prop="deviceName" label="设备名称" width="130"></el-table-column>
            <el-table-column prop="brand" label="品牌" width="130"></el-table-column>
            <el-table-column prop="model" label="规格型号" width="130"></el-table-column>
            <el-table-column prop="remarks" label="备注" width="180"></el-table-column>
            <el-table-column label="操作" width="330">
              <template #default="scope">
                <el-link  style="margin: 0px 5px" type="primary" @click="deleteTask(scope.row)" v-if="isDialog"><el-icon><Delete /></el-icon>删除</el-link>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
              @current-change="handleCurrentChangeOfLedgers"
              :current-page="currentPageOfLedgers"
              :page-size="pageSize"
              :total="deviceMainPlanOfledgers.length"
              layout="total, prev, pager, next, jumper"
              style="margin-top: 20px;">
          </el-pagination>
        </el-tab-pane>
        <el-tab-pane label="点检项目">
          <el-button type="primary" @click="addMainPlanOfItems" v-if="isDialog">新增</el-button>
          <el-button type="danger" @click="deleteSelectionPlanOfItem" v-if="isDialog">删除</el-button>
          <el-table :data="deviceMainPlanOfItems" style="margin-top: 20px;" @selection-change="handleSelectionChangeofItem">
            <el-table-column
                type="selection"
                width="55">
            </el-table-column>
            <el-table-column prop="projectCode" label="项目编码" width="130"></el-table-column>
            <el-table-column prop="projectName" label="项目名称" width="130"></el-table-column>
            <el-table-column prop="projectType" label="项目类型" width="130"></el-table-column>
            <el-table-column prop="projectContent" label="项目内容" width="180"></el-table-column>
            <el-table-column prop="standard" label="标准" width="180"></el-table-column>
            <el-table-column label="操作" width="330">
              <template #default="scope">
                <el-link  style="margin: 0px 5px" type="primary" @click="deleteTask(scope.row)" v-if="isDialog"><el-icon><Delete /></el-icon>删除</el-link>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
              @current-change="handleCurrentChangeOfItems"
              :current-page="currentPageOfItems"
              :page-size="pageSize"
              :total="deviceMainPlanOfItems.length"
              layout="total, prev, pager, next, jumper"
              style="margin-top: 20px;">
          </el-pagination>
        </el-tab-pane>
      </el-tabs>

      <!-- 表单操作按钮 -->
      <el-form-item>
        <el-button type="primary" @click="EditsubmitForm" v-if="isDialog">保存</el-button>
        <el-button type="primary" @click="EditsubmitFormStop" v-if="deviceMainPlanFrom.planStatus == 4" >停用</el-button>
        <el-button type="primary" @click="EditsubmitFormStart" v-if="deviceMainPlanFrom.planStatus != 4 && isDialog">启用</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>

  </el-dialog>

  <!--详情信息-->
  <el-dialog v-model="dialogVisibleDetail" title="设备信息">
    <div>
      <p><strong>设备编码:</strong> {{ selectedDevice.deviceCode }}</p>
      <p><strong>设备名称:</strong> {{ selectedDevice.deviceName }}</p>
      <p><strong>品牌:</strong> {{ selectedDevice.brand }}</p>
      <p><strong>型号:</strong> {{ selectedDevice.model }}</p>
      <p><strong>备注:</strong> {{ selectedDevice.remarks }}</p>
      <p><strong>车间名字:</strong> {{ selectedDevice.workshopName }}</p>
    </div>
    <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleDetail = false">取 消</el-button>
      </span>
  </el-dialog>

  <el-dialog title="项目计划" v-model="deviceMainPlanOfItemsView" @close="resetSelection">
    <el-row style="margin: 0px 10px;padding: 0px 15px">
      <el-col :span="24">
        <el-table
            :data="deviceMainItems"
            border
            stripe
            row-key="projectCode"
            @selection-change="handleSelectionChangOfItems"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="projectCode" label="项目编码" />
          <el-table-column prop="projectType" label="项目类型">
            <template v-slot="scope">
                <span>
                  {{ scope.row.projectType === 1 ? '设备保养' : '设备检点' }}
                </span>
            </template>
          </el-table-column>
          <el-table-column prop="projectContent" label="项目内容" />
          <el-table-column prop="standard" label="标准" />
          <el-table-column label="是否启用">
            <template #default="scope">
              <span>{{ scope.row.enabled ? '是' : '否' }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closedeviceMainPlanOfItemsView">取 消</el-button>
      <el-button type="primary" @click="saveSelectionOfItem">确 定</el-button>
    </div>
  </el-dialog>

  <el-dialog title="设备选择" v-model="deviceMainPlanOfledgerView" @close="resetSelection">
    <el-row style="margin: 0px 10px;padding: 0px 15px">
      <el-col :span="24">
        <el-table :data="deviceMainledgers" border stripe
                  row-key="deviceTypeCode"
                  ref="taskTable"
                  @selection-change="handleSelectionChangOfledgers">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="deviceCode" label="设备编码">
            <template #default="scope">
              <el-button @click="viewDeviceInfo(scope.row)" type="text">{{ scope.row.deviceCode }}</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="deviceName" label="设备名称"/>
          <el-table-column prop="brand" label="品牌"/>
          <el-table-column prop="model" label="规格型号"/>
          <el-table-column prop="workshopName" label="所属车间"/>
          <el-table-column prop="deviceStatus" label="设备状态"/>
          <el-table-column
              prop="createdAt"
              label="创建时间"
          />
        </el-table>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closedeviceMainPlanOfledgerView">取 消</el-button>
      <el-button type="primary" @click="saveSelectionOfledger">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import {ElMessage, ElMessageBox} from "element-plus";
import deviceRequest from "@/api/device.js";
import * as XLSX from 'xlsx';
import {SemiSelect} from "@element-plus/icons-vue";

let workshops = ref([]); // 初始为 null\
let categorys = ref([]); // 初始为 null\
let selectedTask = ref(({}));
let deciveTypeForm =ref({
  deviceTypeCode: '',
  deviceTypeName: '',
  isEnabled: 1, // 默认启用状态
  remarks: '',
  isParent: 0, // 默认不是父节点
  parentCode: '',
});
let currentCodeIndex = 100;
let deviceMainPlanFrom = ref({});
let deviceMainPlanQuery=ref({});
let EmptyQuery=ref({});
let selectedledgerId = ref([]);
let selectedItemId = ref([]);
let deviceMainItems = ref([]);
let deviceMainledgers=ref([]);
let deviceMainPlanOfItems = ref([]);
let deviceMainPlanOfledgers=ref([]);
let selectedDevice= ref({});
let currentPageOfLedgers=ref(1);
let currentPageOfItems=ref(1);
let pageSize=ref(10);
let selectedItems = ref([]);
let selectedledgers= ref([]);
const resetForm = () => {
  deviceMainPlanFrom.value = {
  };
  isDialog.value=true;
};
const route = useRoute();
const columns = [
  { prop: 'task_id', label: '任务编号' },
  { prop: 'task_title', label: '任务标题' },
  { prop: 'task_desc', label: '任务描述' },
  { prop: 'start_time', label: '开始时间' },
  { prop: 'end_time', label: '结束时间' },
  { prop: 'task_duration', label: '工期' },
  { prop: 'task_progress', label: '进度' },
];
const project = ref({});
const users = ref({});
const deviceMainPlans = ref([]);
const curUserId = ref(-1);
const dialogVisible = ref(false);
const EditdialogVisible = ref(false);
const dialogVisibleDetail = ref(false);
const deviceMainPlanOfItemsView = ref(false);
const deviceMainPlanOfledgerView = ref(false);
const editTask = ref({});
const isDialog = ref(true);
const defaultProps = {
  children: 'children',
  label: 'task_title',
};

const isBatchEdit = ref(false);
const openEditDialog = (row) => {
  deviceMainPlanFrom.value=row;
  deviceRequest.loadDeviceMainPlanOfItems(deviceMainPlanFrom.value.planCode,1,10,res=>{
    deviceMainPlanOfItems.value = res.data;
  })
  deviceRequest.loadDeviceMainPlanOfledgers(deviceMainPlanFrom.value.planCode,1,10,res=>{
    deviceMainPlanOfledgers.value = res.data;
  })

  EditdialogVisible.value = true;
};
const openDialog = (row) => {
  deviceMainPlanFrom.value=row;
  deviceRequest.loadDeviceMainPlanOfItems(deviceMainPlanFrom.value.planCode,1,10,res=>{
    deviceMainPlanOfItems.value = res.data;
  })
  deviceRequest.loadDeviceMainPlanOfledgers(deviceMainPlanFrom.value.planCode,1,10,res=>{
    deviceMainPlanOfledgers.value = res.data;
  })
  isDialog.value = false;
  EditdialogVisible.value = true;
};
const deleteTask = (row) =>{


  console.log(row.deviceCode);
  ElMessageBox.confirm(
      '您确定要将任务删除吗？',
      '提示',
      {
        confirmButtonText: '是的',
        cancelButtonText: '点错了',
        type: 'warning',
      }
  ).then(()=>{

    if(!row.parent){
      deviceRequest.deleteDeviceledger(row.deviceCode,res=>{
        loadTasks(0);
        window.location.reload();
      })
    }else {
      ElMessage({
        type:"warning",
        message:"您选中的项目包含子项目，请先删除子项目"
      });
      return;
    }
  })
};
onMounted(() => {
  loadTasks(0);
  loadWorkshops();
  loadCategorys();
  loaddeviceMainledgers();
  loaddeviceMainItems();
});
function handleSelectionChangOfItems(val){
  selectedItems.value=val;
}
function handleSelectionChangOfledgers(val){
  selectedledgers.value=val;
}
function saveSelectionOfledger(){
  console.log(selectedledgers.value);
  console.log(deviceMainPlanFrom.value.planCode);
  deviceRequest.addDeviceMainPlanOfledgers(selectedledgers.value,deviceMainPlanFrom.value.planCode,res=>{
    deviceMainPlanOfledgerView.value=false;
    loaddeviceMainledgers();
  })
}
function saveSelectionOfItem(){
  console.log(selectedItems.value);
  console.log(deviceMainPlanFrom.value.planCode);
  deviceRequest.addDeviceMainPlanOfItems(selectedItems.value,deviceMainPlanFrom.value.planCode,res=>{
    deviceMainPlanOfItemsView.value=false;
    loaddeviceMainItems();
  })
}
function resetSelection() {
  selectedItems.value = []; // 重置选中的项目
}
function closedeviceMainPlanOfItemsView(){
  deviceMainPlanOfItemsView.value=false;
}
function closedeviceMainPlanOfledgerView(){
  deviceMainPlanOfledgerView.value=false;
}
function deleteMainPlanOfItems(){
}
function addMainPlanOfItems(){
    deviceMainPlanOfItemsView.value=true;
    console.log(deviceMainPlanOfItemsView.value);
}
function deleteMainPlanOfledgers(){

}
function addMainPlanOfledgers(){
  deviceMainPlanOfledgerView.value=true;
}
function loaddeviceMainItems(){
  deviceRequest.loadDeviceMainItems(EmptyQuery,1,10,res=>{
    deviceMainItems.value = res.data;
    console.log(deviceMainItems.value);
  })
}
function loaddeviceMainledgers(){
  deviceRequest.loadDeviceledger(EmptyQuery,1,10,res=>{
    deviceMainledgers.value = res.data;
    console.log(deviceMainledgers.value);
  })
}
function exportToExcel() {
  // 转换数据格式为 Excel 所需的格式
  const worksheet = XLSX.utils.json_to_sheet(deviceMainPlans.value);
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, '设备信息');
  // 导出 Excel 文件
  XLSX.writeFile(workbook, '设备信息.xlsx');
}
function viewDeviceInfo(device){
  selectedDevice.value = device;
  console.log(selectedDevice.value);
  dialogVisibleDetail.value = true;
}
function loadCategorys(){
  deviceRequest.loadCategorys(res=>{
    categorys.value = res.data;
  })
}
function loadWorkshops(){
  deviceRequest.workshoplist(res=>{
    workshops.value = res.data;
    console.log("workshop集合"+workshops.value);
  });
}
function EditsubmitForm(){

    console.log(deviceMainPlanFrom.value);
    deviceRequest.EditMainTainPlanForm(deviceMainPlanFrom.value,res=>{
      loadTasks(0);
      EditdialogVisible.value=false;
    });
}
function EditsubmitFormStart(){
  deviceMainPlanFrom.value.planStatus="4";
  console.log(deviceMainPlanFrom.value);
  deviceRequest.EditMainTainPlanForm(deviceMainPlanFrom.value,res=>{
    loadTasks(0);
    EditdialogVisible.value=false;
  });
}
function EditsubmitFormStop(){
  deviceMainPlanFrom.value.planStatus="0";
  console.log(deviceMainPlanFrom.value);
  deviceRequest.EditMainTainPlanForm(deviceMainPlanFrom.value,res=>{
    loadTasks(0);
    EditdialogVisible.value=false;
  });
}
function generateDeviceCode(){
  deviceMainPlanFrom.value.planCode = `PLAN${currentCodeIndex}`;
  currentCodeIndex++; // 增加索引以便下次生成下一个编码
}
function submitForm(){
  console.log(deviceMainPlanFrom.value);
  deviceRequest.addDeviceMainPlanFrom(deviceMainPlanFrom.value,res=>{
    loadTasks(0);
    dialogVisible.value=false;
  })
}
function handleSelectionChangeofItem(val){
  selectedItemId.value=val;
}
function handleSelectionChangeofledger(val){
  selectedledgerId.value=val;
  console.log(selectedledgerId.value);
}
function deleteSelectionPlanOfItem(){
  deviceRequest.deleteSelectionPlanOfItem(selectedItemId.value,deviceMainPlanFrom.value.planCode,res=>{
    loaddeviceMainItems();
    location.reload(); // 刷新整个页面
  })
}
function deleteSelectionPlanOfledger(){
  console.log(selectedledgerId.value);
  deviceRequest.deleteSelectionPlanOfledger(selectedledgerId.value,deviceMainPlanFrom.value.planCode,res=>{
    loaddeviceMainledgers();
    location.reload(); // 刷新整个页面
  })
}
function addSonTask(select){
  selectedTask.value=select;
  console.log("选中的数据",selectedTask.value);
  dialogVisible.value= true;

}
function  addTask(){
  dialogVisible.value = true;
}
function batchDeleteTask() {
}
function batchEditTask(){
  isBatchEdit.value=true;
  editTask.value={};
  EditdialogVisible.value = true;

}
function loadTasks(parentId) {

  deviceRequest.loaddeviceMainPlans(deviceMainPlanQuery.value,1,10,res=>{
    deviceMainPlans.value=res.data;
    console.log(deviceMainPlans);
  })
}
function loadChildrenTask(deviceType,node,resole){
  console.log(deviceType.deviceTypeCode);
  console.log(node);
  deviceRequest.loaddeviceMainPlans(deviceMainPlanQuery.value,deviceType.deviceTypeCode,1,10,res=>{
    resole(res.data);
  })
}
function handleCurrentChangeOfItems(newPage) {
  currentPageOfItems.value = newPage;
}
function handleCurrentChangeOfLedgers(newPage) {
  currentPageOfLedgers.value = newPage;
}
</script>
